# coding=utf-8
import json
import re
import urllib

MONTH = {'Dec': '12', 'Jan': '01', 'Oct': '10', 'Nov': '11'}
DOWNLOAD_TYPE = {'0': '手动', '1': '自动'}


def parse_url(groups):
    tmp = {}
    tmp['user_ip'] = groups[0]
    time_groups = re.match('(.*)/(.*)/(.*):(.*):(.*):(.*)', groups[1])
    tmp['create_time'] = '%s-%s-%s %s:%s:%s' % (time_groups[3], MONTH[time_groups[2]],
                                                time_groups[1], time_groups[4], time_groups[5], time_groups[6])
    try:
        url = groups[2].split(' ')[0]
        param = url[url.index('?') + 1:]
        for item in param.split('&'):
            group = item.split('=')
            tmp[group[0]] = urllib.parse.unquote(group[1])
        tmp['count'] = 1
        tmp['auto'] = DOWNLOAD_TYPE[tmp.get('auto') or '0']
        tmp['update_time'] = ''
    except Exception as e:
       print(groups)
    return get_download_key(tmp), get_target_key(tmp), format_param(tmp)


def format_param(item):
    tmp = {}
    for key, val in dict(item).items():
        if isinstance(val, str):
            tmp[key] = urllib.parse.unquote(val)
        else:
            tmp[key] = val
    return tmp


def get_download_key(item):
    return '%s:%s:%s:%s:%s' % (item['product'], item['imei'], item['old_version'], item['new_version'], 2)


def get_target_key(item):
    return '%s:%s:%s:%s:%s' % (item['product'], item['imei'], item['new_version'], item['new_version'], 2)


def read_file(file_p):
    tmp = {}
    with open(file_p, 'r+', encoding='utf-8') as rf:
        for item in rf:
            rst = re.match('(.*) - - \[(.*) \+0800\] "GET (/fota-version/download/[^"]*\?[^ ][^"]+).*', item)
            if rst is not None:
                if 'x-zip-compressed' in rst.group(3):
                    continue
                key, target_key, value = parse_url(rst.groups())
                old_val = tmp.get(key)
                if old_val is not None:
                    value['update_time'] = value['create_time']
                    value['create_time'] = old_val['create_time']
                    value['count'] += old_val['count']
                tmp[key] = value
        return tmp


def output_file(data):
    rows = ['IMEI,产品名称,当前版本,目标版本,用户IP,响应服务器,计数,创建时间,更新时间,操作类型,下载类型,状态\n']
    for key, val in data.items():
        rows.append('{imei},{product},{old_version},{new_version},{user_ip},中国服务器,{count},{create_time},{update_time},下载,{auto},成功\n'.format(**val))

    with open('./rst1.csv', 'w+', encoding='gbk') as wf:
        wf.writelines(rows)


if __name__ == '__main__':
    result = read_file('../demo1.txt')
    print(json.dumps(result))
    output_file(result)
    # print('{a}:{b}'.format(**{'a': 'a1', 'b': 'b1'}))
